clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/
global output /Users/zachbrown/Projects/PriceTransparency/Output


// open visit dataset
use clean/radiology_visit_3.dta, clear


// Make treatment indicator
gen post = (ym>=ym(2007,3))
gen web_post = (visit_on_web==1 & post==1)
label var web_post "$\text{OnWeb}\textsubscript{m}\times\text{Post}\textsubscript{t}$"


//////////////////////////////////////////////////////////////
// Probability of going to same provider as in the past
//////////////////////////////////////////////////////////////
preserve
sort member_key from_date
bys member_key (from_date): gen visit_num = _n
tsset member_key visit_num
gen last_prov_id = l.prov_id
gen last_proc_class = l.proc_class
gen same_prov_as_last = prov_id ==last_prov_id 
replace same_prov_as_last=. if last_prov_id==.
bys web_post: summ same_prov_as_last
global ctr_vars = "age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.year i.std_product_type i.payer_id"

areg same_prov_as_last web_post $ctr_vars, absorb(proc_id_radio) vce(cl ym) 
eststo a1

restore

//////////////////////////////////////////////////////////////
// Probability of going to provider in lowest quantile in county
//////////////////////////////////////////////////////////////
preserve
drop if prov_id==.

// Mode zip
rename serv_prov_zip tmp_serv_prov_zip
bys prov_id year: egen serv_prov_zip=mode(tmp_serv_prov_zip), missing
drop tmp_serv_prov_zip

// Merge on county
rename member_zip zip
merge m:1 zip using dist/zcta_county_rel_10.dta
rename zip member_zip
rename county member_county
drop _merge

replace member_county=-1 if member_county==.

// Merge on quartiles
merge m:1 member_county proc_id_radio year payernm_short using clean/price_ptiles.dta
drop _merge

global ctr_vars = "age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.year"

gen below_oopr_10 = amt_oop_radio < amt_oopr_10
replace below_oopr_10=. if amt_oopr_10==.
areg below_oopr_10 web_post $ctr_vars, absorb(proc_id_radio) vce(cl ym) 
gen below =  (below_oopr_10==1)
eststo a2
restore




//////////////////////////////////////////////////////////////
// Provider in NH
//////////////////////////////////////////////////////////////

global ctr_vars1 = "has_copay has_coin##deduct_type age male i.charlindex zip_mean_income zip_pct_ba i.rural_class i.ym i.std_product_type i.payer_id"

gen prov_in_nh2 = (prov_in_nh~=0)
areg prov_in_nh2 web_post $ctr_vars1, absorb(proc_id_radio) vce(cl ym) 
eststo a3


//////////////////////////////////////////////////////////////
// Distance
//////////////////////////////////////////////////////////////

tempfile tmpdata
shell nice gunzip dist/radiology_dist.dta -c > `tmpdata'
merge m:1 rec_id_radio using `tmpdata'
drop if _merge==2
drop _merge


replace bill_dist_mi = 100 if bill_dist_mi>100
areg bill_dist_mi web_post $ctr_vars1, absorb(proc_id_radio) vce(cl ym) 
eststo a4



esttab a1 a2 a3 a4 using $output/search_regs_a.tex, frag cells(b(fmt(4) star) se(fmt(4) par)) ///
	keep(web_post )  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 

esttab a1 a2 a3 a4 using $output/search_regs_b.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ar2 N, fmt(3 %12.0fc) labels("Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 
